package com.hdax.dm.service;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.hdax.dm.config.AliPayConfig;
import com.hdax.dm.entity.order.Order;
import com.hdax.dm.entity.order.OrderLinkUser;
import com.hdax.dm.entity.scheduler.SchedulerSeat;
import com.hdax.dm.feign.order.OrderFeign;
import com.hdax.dm.feign.order.OrderLinkUserFeign;
import com.hdax.dm.feign.scheduler.SchedulerSeatFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Service
public class PayService {

    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private OrderLinkUserFeign orderLinkUserFeign;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private SchedulerSeatFeign schedulerSeatFeign;


    public String pay(String orderNo) throws Exception{

        Order order = orderFeign.FindByOrderId(orderNo);
        Double price= order.getTotalAmount();
        String subject = order.getItemName();
        List<OrderLinkUser> linkUsers = orderLinkUserFeign.findLinkUserByOrderId(order.getId());
        StringBuilder builder = new StringBuilder();
        linkUsers.forEach(linkUser->{
            builder.append(linkUser.getX()+"排"+linkUser.getY()+",");
        });
        builder.append("共"+order.getTotalCount()+"张!");
        String description = builder.toString();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AliPayConfig.return_url);
        alipayRequest.setNotifyUrl(AliPayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderNo +"\","
                + "\"total_amount\":\""+ price +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ description +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");


        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println(result);
        return result;
    }

    public RedirectView success(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        //获取订单号
        String orderNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");

        //删除redis订单
        redisTemplate.delete("orderNo:"+orderNo);

        //订单状态修改
        orderFeign.updateOrderType(orderNo,2);

        //座位状态修改为已售出
        boolean b = schedulerSeatFeign.updateSeatStatusSuccess(orderNo, 3);
        System.out.println("异步");
        //转发到首页
        return new RedirectView("http://localhost:8080");
    }


}
